package tutorial.form;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.stereotype.Component;
import org.springmodules.validation.bean.conf.loader.annotation.handler.NotBlank;
import org.springmodules.validation.bean.conf.loader.annotation.handler.NotNull;
import org.springmodules.validation.bean.conf.loader.annotation.handler.RegExp;

import tutorial.domain.User;

@Component("userForm")
public class UserForm {
	@NotNull
	@NotBlank
	@RegExp(value = "[\\d]*")
	private String idUser;
	@NotNull
	@NotBlank
	private String ime;
	@NotNull
	@NotBlank
	private String prezime;

	@NotNull
	@NotBlank
	private String password;
	@NotNull
	@NotBlank
	@RegExp(value = "\\d{2}\\.\\d{2}\\.\\d{4}\\.")
	private String datum;

	public String getIdUser() {
		return idUser;
	}

	public void setIdUser(String idUser) {
		this.idUser = idUser;
	}

	public String getIme() {
		return ime;
	}

	public void setIme(String ime) {
		this.ime = ime;
	}

	public String getPrezime() {
		return prezime;
	}

	public void setPrezime(String prezime) {
		this.prezime = prezime;
	}

	public String getDatum() {
		return datum;
	}

	public void setDatum(String datum) {
		this.datum = datum;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public User fetchDomainObject() throws ParseException {
		User user = new User();
		user.setIme(this.getIme());
		user.setPrezime(this.getPrezime());
		user.setPassword(this.getPassword().hashCode());
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy.");
		Date datazur = dateFormat.parse(this.getDatum());
		user.setDatazur(datazur);

		return user;
	}
}
